package oa.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import oa.service.ApplicationService;
import oa.service.CompanyService;
import oa.service.UserService;
import oa.service.impl.ApplicationServiceImpl;
import oa.service.impl.CompanyServiceImpl;
import oa.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;

import oa.entity.*;
import oa.exception.DaoException;
/**
 * Servlet implementation class applicationlist
 */
@WebServlet("/applicationlist")
public class applicationlist extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public applicationlist() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		UserService user = new UserServiceImpl();
		ApplicationService  Application = new ApplicationServiceImpl();
		CompanyService comps=new CompanyServiceImpl();
		HttpSession session = request.getSession();
	    PrintWriter out= response.getWriter();
	    List<Application> ApplicationList = new ArrayList<Application>();
	    List<User> userList = new ArrayList<User>();
	    List<Company> comp=new  ArrayList<Company>();
	    boolean flag = false;
	    String opt = request.getParameter("opt");
	    String del=request.getParameter("del");
		String tid=request.getParameter("tid");
		String status = request.getParameter("status");
		//点击添加
	    if("applicationadd".equals(opt)) {
	    	try {
	    		SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
				Date date1 = sf1.parse(sf1.format(new Date()));			
	    		int userid= Integer.parseInt(request.getParameter("userid"));
	    		String title=request.getParameter("val-terms");
		    	int companyid = Integer.parseInt(request.getParameter("val-company"));
		    	String type = request.getParameter("val-type");
		    	String control = request.getParameter("var-text");   		  
		    	Application  applic = new Application();
		    	applic.setUserId(userid);
		    	applic.setApplicationTitle(title);
		    	applic.setApplicationReason(control);
		    	applic.setApplicationType(type);
		    	applic.setCompanyId(companyid);
		    	applic.setApplicationTime(date1);
		    	flag=Application.addApplication(applic);
		    	out.print(JSON.toJSONString(flag));
			} catch (Exception e) {
				// TODO: handle exception
			}
	    	
	    	out.flush();
	    	out.close();
	    //获取详细信息
	    }else if("sel".equals(opt)) {
	    	      
	  	        try {
	  	        	  List<Application> applist=Application.selectApplication(Integer.parseInt(tid), null, 0);
	  	  	            userList = user.selectAllUser(null,0,0);
	  				    comp=comps.selectAllCompany(0);
	  			        session.setAttribute("applists",  applist);
	  			        session.setAttribute("UserList", userList);
	  			        session.setAttribute("comp", comp);
	  					request.getRequestDispatcher("Select-Show/application_tabulation.jsp").forward(request, response);
	  			} catch (DaoException e) {
	  				// TODO Auto-generated catch block
	  				e.printStackTrace();
	  			}
	  	  
	  		
	    }
	   //点击删除
	    else if("del".equals(opt)){
			 flag=Application.deleteApplication(Integer.parseInt(del));
		      out.print(JSON.toJSONString(flag));
		}
	    //拒绝申请
	    else if("applicationRefuse".equals(opt)) {
		try {
			String inputvalue = request.getParameter("inputvalue");
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = sf.parse(sf.format(new Date()));
			Application applicat = new Application();
			applicat.setApplicationStatus(3);
			applicat.setApplicationTimes(date);
			applicat.setApplicationRefuse(inputvalue);
			applicat.setApplicationId(Integer.parseInt(tid));		
		    flag =Application.upadteApplication(applicat);
			out.print(JSON.toJSONString(flag));
		} catch (Exception e) {
			// TODO: handle exception
		 }
		//申请通过
		}else if("applicationok".equals(opt)) {
			try {
			    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date date = sf.parse(sf.format(new Date()));
				Application applicat = new Application();
				applicat.setApplicationStatus(1);
				applicat.setApplicationTimes(date);
			    applicat.setApplicationId(Integer.parseInt(tid));		
			    flag =Application.upadteApplication(applicat);
				out.print(JSON.toJSONString(flag));
			} catch (Exception e) {
				// TODO: handle exception
			 }
		}
	    
	    
	    //最终都会执行的查询方法
       else {
    	if(status==null) {
    		ApplicationList=Application.selectAllApplication();
		}else {
			 ApplicationList = Application.selectApplication(0,"",Integer.parseInt(status));
			   
		}
	    userList = user.selectAllUser(null,0,0);
	    session.setAttribute("ApplicationList", ApplicationList);
	    session.setAttribute("UserList", userList);		   
	    request.getRequestDispatcher("application_list.jsp").forward(request, response);
	    }
	    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
